//
//  AYSHomeMoreDataViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/10/28.
//  Copyright (c) 2015年 李晓理. All rights reserved.
//

#import "AYSHomeMoreDataViewController.h"
#import "AYSHomeMoreCollectionViewCell.h"
#import "AYSHomeMoreModel.h"
#import <UIImageView+WebCache.h>
#import "AYSRequestManager.h"
#import "AYSCommonPlayViewController.h"
#import "MJRefresh.h"
#import "SVProgressHUD.h"
@interface AYSHomeMoreDataViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong)UICollectionView *collectionView;// 集合视图
@property(nonatomic, strong)NSMutableArray *moreDataArr;// 更多数据源
@property(nonatomic, assign)NSInteger pageindex;
@end

@implementation AYSHomeMoreDataViewController

// 请求数据
- (void) requestSourceDate
{
    switch (_type)
    {
        case CategoryTypeMovie:
            [self requestDataWithCategoryForDMWithurl:KCategoryForMovie];
            break;
        case CategoryTypeTV:
            [self requestDataWithCategoryForDMWithurl:KCategoryForTV];
            break;
        case CategoryTypeCarton:
            [self requestDataWithCategoryForDMWithurl:KCategoryForDM];
            break;
        case CategoryTypeZY:
            [self requestDataWithCategoryForDMWithurl:KCategoryForZY];
            break;
        default:
            break;
    }
}

#pragma wark ==============================刷新数据请求
- (void)requestDataWithCategoryForZYWithurl:(NSString *)url
{
    [AYSRequestManager requestWithType:GET url:url parDic:nil finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *resultArr = dic[@"result"];
        for (NSDictionary *resultDic in resultArr)
        {
            AYSHomeMoreModel *model = [[AYSHomeMoreModel alloc] init];
            [model setValuesForKeysWithDictionary:resultDic];
            [_moreDataArr addObject:model];
        }
        
    } error:^(NSError *error)
     {
         NSLog(@"更多综艺网络请求失败");
     }];
    
}

#pragma wark ============================更多数据请求
- (void)requestDataWithCategoryForDMWithurl:(NSString *)url
{
    [AYSRequestManager requestWithType:GET url:url parDic:nil finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *resultArr = dic[@"result"];
        for (NSDictionary *resultDic in resultArr)
        {
            AYSHomeMoreModel *model = [[AYSHomeMoreModel alloc] init];
            [model setValuesForKeysWithDictionary:resultDic];
            [_moreDataArr addObject:model];
        }
        [self getCollectionView];
        [SVProgressHUD dismiss];

    } error:^(NSError *error)
     {
         NSLog(@"更多动漫网络请求失败");
     }];
    
   
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.titleLabel.frame = CGRectMake(SCREEN_WIDTH / 2 - 20, 35, SCREEN_WIDTH / 2, 20);
    switch (_type)
    {
        case CategoryTypeMovie:
            self.titleLabel.text = @"电影";
            break;
        case CategoryTypeTV:
            self.titleLabel.text = @"电视剧";
            break;
        case CategoryTypeCarton:
            self.titleLabel.text = @"动漫";
            break;
        case CategoryTypeZY:
            self.titleLabel.text = @"综艺";
            break;
        default:
            break;
    }
    
    self.moreDataArr = [NSMutableArray arrayWithCapacity:0];
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBar.hidden = YES;
    [self topView];
    [SVProgressHUD show];
    [self requestSourceDate];
    
}

#pragma wark ================collectionView刷新
- (void)refreshData
{
        _pageindex = 1;
        __weak UICollectionView *collectionView = self.collectionView;
        [collectionView.header beginRefreshing];
        // 下拉刷新
        collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self requestSourceDate];
            [collectionView.header endRefreshing];
            
        }];
    
        // 上拉刷新
        collectionView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            
            switch (_type)
            {
                case CategoryTypeMovie:
                {
                    _pageindex ++;
                    NSString *urlStr = [NSString stringWithFormat:@"http://doukantv.com/api/top/?pagesize=20&ver=2.3.4&sys_ver=9.1&type=movie&pageindex=%ld&cli=iphone", _pageindex];
                    [self requestDataWithCategoryForZYWithurl:urlStr];
                }
                    break;
                case CategoryTypeTV:
                {
                    _pageindex ++;
                    NSString *urlStr = [NSString stringWithFormat:@"http://doukantv.com/api/top/?pagesize=20&ver=2.3.4&sys_ver=9.1&type=tv&pageindex=%ld&cli=iphone", _pageindex];
                    [self requestDataWithCategoryForZYWithurl:urlStr];
                }
                    break;
                case CategoryTypeCarton:
                {
                    _pageindex ++;
                    NSString *urlStr = [NSString stringWithFormat:@"http://doukantv.com/api/top/?pagesize=20&ver=2.3.4&sys_ver=9.1&type=katong&pageindex=%ld&cli=iphone", _pageindex];
                    [self requestDataWithCategoryForZYWithurl:urlStr];
                }
                    break;
                case CategoryTypeZY:
                {
                    _pageindex ++;
                    NSString *urlStr = [NSString stringWithFormat:@"http://doukantv.com/api/top/?pagesize=20&ver=2.3.4&sys_ver=9.1&type=zy&pageindex=%ld&cli=iphone", _pageindex];
                    [self requestDataWithCategoryForZYWithurl:urlStr];
                }
                    break;
                default:
                    break;
            }
            [collectionView reloadData];
            // 结束刷新
            [collectionView.footer endRefreshing];
            
        }];
        // 默认先隐藏footer
        collectionView.footer.hidden = YES;
        
   
}

- (void)getBackToHomeMainVCButton:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma wark ==================collectionView的相关设置
- (void)getCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 20;
    layout.itemSize = CGSizeMake(95, 170);
    layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:layout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [self.view addSubview:_collectionView];
    
    // 注册
    [_collectionView registerClass:[AYSHomeMoreCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self refreshData];
    
}

#pragma wark ==================collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _moreDataArr.count; 
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AYSHomeMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    AYSHomeMoreModel *model = _moreDataArr[indexPath.row];
    [cell.picture sd_setImageWithURL:[NSURL URLWithString:model.vthumburl]];
    cell.nameLabel.text = model.programname;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AYSCommonPlayViewController *commonPlayVC = [[AYSCommonPlayViewController alloc] init];
    AYSHomeMoreModel *model = _moreDataArr[indexPath.row];
    commonPlayVC.programeID = model.programID;
   // commonPlayVC.navigationController = self.navigationController;
    [self.navigationController pushViewController:commonPlayVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
